package com.dingwen.treasure.business.component.listener.disinfect;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.dingwen.treasure.base.constant.RabbitmqConstant;
import com.dingwen.treasure.base.enums.MsgState;
import com.dingwen.treasure.business.component.event.SubscribeDisinfectServerEvent;
import com.dingwen.treasure.model.entity.mybatisplus.TreasureMessage;
import com.dingwen.treasure.mybatisplus.service.TreasureMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 　SubscribeListener: 订阅
 * 　@author dingwen
 * 　@date 2022/8/9
 */
@Component
@Slf4j
@Order(10)
public class SubscribeListener implements ApplicationListener<SubscribeDisinfectServerEvent> {


    /**
     * messageService
     */
    @Resource
    private TreasureMessageService messageService;

    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(SubscribeDisinfectServerEvent event) {
        log.info("消杀服务订阅事件,订阅执行");
        Long businessId = Convert.toLong(event.getSource());
        TreasureMessage treasureMessage = TreasureMessage.builder()
                .businessId(businessId)
                .businessDescription("消杀服务订阅事件,订阅执行")
                .msgBody(JSONUtil.toJsonStr(this))
                .msgState(MsgState.SAVED)
                .queue(RabbitmqConstant.QUEUE_DISINFECT_SERVER)
                .router(RabbitmqConstant.ROUTER_KEY_DISINFECT_SERVER)
                .businessOrder(1)
                .build();

        messageService.save(treasureMessage);
    }
}
